package com.ybkj.o2o.zhuanche.manager.impl;

import com.ybkj.o2o.zhuanche.common.DataGrid;
import com.ybkj.o2o.zhuanche.common.query.CarTypeCompanyQuery;
import com.ybkj.o2o.zhuanche.entity.CarTypeCompany;
import com.ybkj.o2o.zhuanche.entity.ServiceType;
import com.ybkj.o2o.zhuanche.manager.CarTypeCompanyManager;
import com.ybkj.o2o.zhuanche.service.CarTypeCompanyService;
import com.ybkj.o2o.zhuanche.service.ServiceTypeService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DefaultCarTypeCompanyManager implements CarTypeCompanyManager {

  @Autowired
  private CarTypeCompanyService carTypeCompanyService;

  @Autowired
  private ServiceTypeService serviceTypeService;

  @Override
  public DataGrid<CarTypeCompany> findAll(int page, int rows) {
    int offset = (page - 1) * rows;
    RowBounds rowBounds = new RowBounds(offset, rows);

    List<CarTypeCompany> carType = carTypeCompanyService.findAll(rowBounds);
    long total = carTypeCompanyService.count();

    DataGrid<CarTypeCompany> dataGrid = new DataGrid<>();
    dataGrid.setRows(carType);
    dataGrid.setTotal(total);

    return dataGrid;
  }

  @Override
  public DataGrid<CarTypeCompany> findByCarTypeId(CarTypeCompanyQuery carTypeCompanyQuery) {

    List<CarTypeCompany> list =
        carTypeCompanyService.findByCompanyIdAndCarTypeId(carTypeCompanyQuery);

    for (CarTypeCompany carTypeCompany : list) {
      List<ServiceType> list2 =
          serviceTypeService.findByCarTypeCompanyId(carTypeCompany.getId(),
              carTypeCompanyQuery.getAppKey());
      for (ServiceType serviceType : list2) {
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.tangzu.name().equals(
            serviceType.getName())) {
          carTypeCompany.setTangzuAreaId(serviceType.getAreaId());
          carTypeCompany.setTangzuAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.rizu.name().equals(
            serviceType.getName())) {
          carTypeCompany.setRizuAreaId(serviceType.getAreaId());
          carTypeCompany.setRizuAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.banrizu.name().equals(
            serviceType.getName())) {
          carTypeCompany.setBanrizuAreaId(serviceType.getAreaId());
          carTypeCompany.setBanrizuAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jieji.name().equals(
            serviceType.getName())) {
          carTypeCompany.setJiejiAreaId(serviceType.getAreaId());
          carTypeCompany.setJiejiAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songji.name().equals(
            serviceType.getName())) {
          carTypeCompany.setSongjiAreaId(serviceType.getAreaId());
          carTypeCompany.setSongjiAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jiezhan.name().equals(
            serviceType.getName())) {
          carTypeCompany.setJiezhanAreaId(serviceType.getAreaId());
          carTypeCompany.setJiezhanAreaName(serviceType.getAreaName());
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songzhan.name().equals(
            serviceType.getName())) {
          carTypeCompany.setSongzhanAreaId(serviceType.getAreaId());
          carTypeCompany.setSongzhanAreaName(serviceType.getAreaName());
        }
      }
    }

    Long count = carTypeCompanyService.countByCarTypeId(carTypeCompanyQuery);

    DataGrid<CarTypeCompany> dataGrid = new DataGrid<>();
    dataGrid.setRows(list);
    dataGrid.setTotal(count);

    return dataGrid;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void create(Long carTypeId, Long companyId, String companyName, Integer sequence,
      Long tangzuAreaId, Long rizuAreaId, Long banrizuAreaId, Long jiejiAreaId, Long songjiAreaId,
      Long jiezhanAreaId, Long songzhanAreaId, String tangzuAreaName, String rizuAreaName,
      String banrizuAreaName, String jiejiAreaName, String songjiAreaName, String jiezhanAreaName,
      String songzhanAreaName, String appKey) {

    CarTypeCompany carTypeCompany = new CarTypeCompany();

    carTypeCompany.setCarTypeId(carTypeId);
    carTypeCompany.setCompanyId(companyId);
    carTypeCompany.setCompanyName(companyName);
    carTypeCompany.setSequence(sequence);
    carTypeCompany.setAppKey(appKey);
    carTypeCompanyService.insert(carTypeCompany);
    Long carTypeCompanyId = carTypeCompany.getId();

    if (tangzuAreaId != null && tangzuAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(tangzuAreaId);
      serverType.setAreaName(tangzuAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.tangzu.name());
      serverType.setTypeName("立即用车");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (rizuAreaId != null && rizuAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(rizuAreaId);
      serverType.setAreaName(rizuAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.rizu.name());
      serverType.setTypeName("日租");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (banrizuAreaId != null && banrizuAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(banrizuAreaId);
      serverType.setAreaName(banrizuAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.banrizu.name());
      serverType.setTypeName("半日租");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (jiejiAreaId != null && jiejiAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(jiejiAreaId);
      serverType.setAreaName(jiejiAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jieji.name());
      serverType.setTypeName("接机");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (songjiAreaId != null && songjiAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(songjiAreaId);
      serverType.setAreaName(songjiAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songji.name());
      serverType.setTypeName("送机");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (jiezhanAreaId != null && jiezhanAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(jiezhanAreaId);
      serverType.setAreaName(jiezhanAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jiezhan.name());
      serverType.setTypeName("接站");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
    if (songzhanAreaId != null && songzhanAreaId != 0) {
      ServiceType serverType = new ServiceType();
      serverType.setAreaId(songzhanAreaId);
      serverType.setAreaName(songzhanAreaName);
      serverType.setCarTypeId(carTypeId);
      serverType.setName(com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songzhan.name());
      serverType.setTypeName("送站");
      serverType.setAppKey(appKey);
      serverType.setCarTypeCompanyId(carTypeCompanyId);
      serviceTypeService.insert(serverType);
    }
  }

  @Override
  public void update(Long carTypeId, Long companyId, Long tangzuAreaId, Long rizuAreaId,
      Long banrizuAreaId, Long jiejiAreaId, Long songjiAreaId, Long jiezhanAreaId,
      Long songzhanAreaId, String tangzuAreaName, String rizuAreaName, String banrizuAreaName,
      String jiejiAreaName, String songjiAreaName, String jiezhanAreaName, String songzhanAreaName,
      String appKey) {
    CarTypeCompanyQuery carTypeCompanyQuery = new CarTypeCompanyQuery();
    carTypeCompanyQuery.setCompanyId(companyId);
    carTypeCompanyQuery.setCarTypeId(carTypeId);
    carTypeCompanyQuery.setAppKey(appKey);
    List<CarTypeCompany> carTypeCompanyList =
        carTypeCompanyService.findByCompanyIdAndCarTypeId(carTypeCompanyQuery);

    if (carTypeCompanyList != null && carTypeCompanyList.size() != 0) {
      List<ServiceType> list =
          serviceTypeService.findByCarTypeCompanyId(carTypeCompanyList.get(0).getId(), appKey);
      for (ServiceType serviceType : list) {
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.tangzu.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(tangzuAreaId);
          serviceType.setAreaName(tangzuAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.rizu.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(rizuAreaId);
          serviceType.setAreaName(rizuAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.banrizu.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(banrizuAreaId);
          serviceType.setAreaName(banrizuAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jieji.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(jiejiAreaId);
          serviceType.setAreaName(jiejiAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songji.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(songjiAreaId);
          serviceType.setAreaName(songjiAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.jiezhan.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(jiezhanAreaId);
          serviceType.setAreaName(jiezhanAreaName);
        }
        if (com.ybkj.o2o.zhuanche.entity.Order.ServiceType.songzhan.name().equals(
            serviceType.getName())) {
          serviceType.setAreaId(songzhanAreaId);
          serviceType.setAreaName(songzhanAreaName);
        }
        serviceTypeService.update(serviceType);
      }
    }
  }

  @Override
  public CarTypeCompany findByCompanyIdAndCarTypeId(CarTypeCompanyQuery carTypeCompanyQuery) {
    List<CarTypeCompany> list =
        carTypeCompanyService.findByCompanyIdAndCarTypeId(carTypeCompanyQuery);
    return list != null && list.size() != 0 ? list.get(0) : null;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void delete(Long carTypeCompanyId, String appKey) {
    List<ServiceType> list = serviceTypeService.findByCarTypeCompanyId(carTypeCompanyId, appKey);
    for (ServiceType serviceType : list) {
      serviceTypeService.delete(serviceType.getId());
    }
    carTypeCompanyService.delete(carTypeCompanyId);
  }
}
